using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Infrastructure.Configuration;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 配置管理示例控制器
    /// 展示如何使用新的配置管理功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ConfigurationExampleController : ControllerBase
    {
        private readonly IAppSettingsManager _appSettingsManager;
        private readonly IConfigurationService _configurationService;

        public ConfigurationExampleController(
            IAppSettingsManager appSettingsManager,
            IConfigurationService configurationService)
        {
            _appSettingsManager = appSettingsManager;
            _configurationService = configurationService;
        }

        /// <summary>
        /// 示例：获取数据库配置
        /// </summary>
        /// <returns>数据库配置信息</returns>
        [HttpGet("database-info")]
        public IActionResult GetDatabaseInfo()
        {
            var dbConfig = _configurationService.GetDatabaseConfig();
            
            return Ok(new
            {
                DatabaseType = dbConfig.Type,
                ConnectionTimeout = dbConfig.ConnectionTimeout,
                CommandTimeout = dbConfig.CommandTimeout,
                Environment = _configurationService.GetEnvironmentName(),
                IsDevelopment = _configurationService.IsDevelopment()
            });
        }

        /// <summary>
        /// 示例：获取缓存配置
        /// </summary>
        /// <returns>缓存配置信息</returns>
        [HttpGet("cache-info")]
        public IActionResult GetCacheInfo()
        {
            var cacheConfig = _configurationService.GetCacheConfig();
            
            return Ok(new
            {
                CacheType = cacheConfig.Type,
                TestEnabled = cacheConfig.TestEnabled,
                TestTTL = cacheConfig.TestTTL,
                RedisUrl = cacheConfig.Redis.Url
            });
        }

        /// <summary>
        /// 示例：获取安全配置
        /// </summary>
        /// <returns>安全配置信息</returns>
        [HttpGet("security-info")]
        public IActionResult GetSecurityInfo()
        {
            var securityConfig = _configurationService.GetSecurityConfig();
            
            return Ok(new
            {
                AppName = securityConfig.AppName,
                EnableMFA = securityConfig.EnableMFA,
                SessionTimeout = securityConfig.SessionTimeout,
                MaxLoginAttempts = securityConfig.MaxLoginAttempts,
                JwtExpiryMinutes = securityConfig.Jwt.ExpiryMinutes
            });
        }

        /// <summary>
        /// 示例：获取应用程序信息
        /// </summary>
        /// <returns>应用程序基础信息</returns>
        [HttpGet("app-info")]
        public IActionResult GetAppInfo()
        {
            var appInfo = _configurationService.GetAppInfo();
            return Ok(appInfo);
        }

        /// <summary>
        /// 示例：动态获取配置值
        /// </summary>
        /// <param name="configKey">配置键</param>
        /// <returns>配置值</returns>
        [HttpGet("config/{configKey}")]
        public IActionResult GetConfigValue(string configKey)
        {
            // 演示不同类型的配置值获取
            var stringValue = _appSettingsManager.GetStringValue(configKey);
            
            // 检查是否为数字类型
            if (int.TryParse(stringValue, out var intValue))
            {
                return Ok(new { key = configKey, value = intValue, type = "integer" });
            }
            
            // 检查是否为布尔类型
            if (bool.TryParse(stringValue, out var boolValue))
            {
                return Ok(new { key = configKey, value = boolValue, type = "boolean" });
            }
            
            return Ok(new { key = configKey, value = stringValue, type = "string" });
        }

        /// <summary>
        /// 示例：获取配置节
        /// </summary>
        /// <param name="sectionName">配置节名称</param>
        /// <returns>配置节内容</returns>
        [HttpGet("section/{sectionName}")]
        public IActionResult GetConfigSection(string sectionName)
        {
            var section = _appSettingsManager.GetSection(sectionName);
            return Ok(section);
        }

        /// <summary>
        /// 示例：健康检查 - 检查关键配置是否正确
        /// </summary>
        /// <returns>健康检查结果</returns>
        [HttpGet("health-check")]
        public IActionResult HealthCheck()
        {
            var healthInfo = new
            {
                DatabaseConfigured = !string.IsNullOrEmpty(_configurationService.GetCurrentDatabaseConnectionString()),
                CacheConfigured = _appSettingsManager.HasKey("Cache:Type"),
                SecurityConfigured = _appSettingsManager.HasKey("Security:Jwt:SecretKey"),
                Environment = _configurationService.GetEnvironmentName(),
                Timestamp = DateTime.UtcNow
            };

            return Ok(healthInfo);
        }
    }
}
